package com.sunstar.liveplugin.util

import com.faceunity.wrapper.faceunity
import java.util.*

class FaceUUtil private constructor() {
    var beautyArray: IntArray? = null
    private var beautyData: ByteArray? = null
    var frameId = 0

    companion object {
        val instance by lazy {
            FaceUUtil()
        }
    }

    init {
        beautyArray = intArrayOf(1)
    }

    fun rebuildFrame(): FaceUUtil {
        frameId = 0
        return this
    }


    fun initBeautyData(beautyData: ByteArray): FaceUUtil {
        this.beautyData = beautyData
        return this
    }

    fun rebuildBeauty(): FaceUUtil {
        beautyArray!![0] = faceunity.fuCreateItemFromPackage(beautyData)
        beautyArray?.run {
            faceunity.fuItemSetParam(this[0], "is_beauty_on", 1.0)
            faceunity.fuItemSetParam(this[0], "color_level", 0.0)
            faceunity.fuItemSetParam(this[0], "red_level", 0.0)
            faceunity.fuItemSetParam(this[0], "blur_level", 0.0)
            faceunity.fuItemSetParam(this[0], "skin_detect", 0.0)
            faceunity.fuItemSetParam(this[0], "nonskin_blur_scale", 0.0)
            faceunity.fuItemSetParam(this[0], "heavy_blur", 0.0)
        }
        return this
    }

    fun initWhite(whiteColorPercent: Double, redColorPercent: Double, blurPercent: Double, filterName: String, filterPercent: Double): FaceUUtil {
        beautyArray?.run {
            faceunity.fuItemSetParam(this[0], "color_level", 2.0 * whiteColorPercent)
            faceunity.fuItemSetParam(this[0], "is_beauty_on", 1.0)
            faceunity.fuItemSetParam(this[0], "red_level", 2.0 * redColorPercent)
            faceunity.fuItemSetParam(this[0], "blur_type", 2.0)
            faceunity.fuItemSetParam(this[0], "blur_level", 6.0 * blurPercent)
            faceunity.fuItemSetParam(this[0], "skin_detect", 1.0)
            faceunity.fuItemSetParam(this[0], "nonskin_blur_scale", 1.0)
            faceunity.fuItemSetParam(this[0], "blur_use_mask", 1.0)
            faceunity.fuItemSetParam(this[0], "heavy_blur", 1.0)
            faceunity.fuItemSetParam(this[0], "filter_name", filterName)
            faceunity.fuItemSetParam(this[0], "filter_level ", 1.0 * filterPercent)
        }
        return this
    }

    fun destory(): FaceUUtil {
//        Arrays.fill(beautyData,0)
        faceunity.fuOnDeviceLost()
        faceunity.fuDestroyAllItems()
        return this
    }

    fun isInit():Boolean = faceunity.fuIsLibraryInit() == 1

}